// pagesMine/pages/brokerList/brokerList.js
import ajax from '../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    formData: {
      gardenId: '',
      phone: '',
      pageSize: 10,
      pageNum: 1,
      total: 0
    }
  },
  /**
   * 重置事件
   */
  resetEvent() {
    this.setData({"formData.phone" : ''})
    this.inputConfirm()
  },
  /**
   * 园区点击事件
   */
  parkEvent(event) {
    if (this.data.formData.gardenId) return
    const item = event.currentTarget.dataset.item
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({ brokerEntity: item })
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('backEvent', item);
    wx.navigateBack()
  },
  /**
   * input变化事件
   */
  inputChange(event) {
    this.setData({ 'formData.phone': event.detail.value })
  },
  /**
   * input确认事件
   */
  inputConfirm() {
    this.data.formData.pageNum = 1
    this.getAgentList()
  },
  async getAgentList() {
    const { dataList, formData } = this.data
    const res = await ajax.get('/my/house/agent/searchList', this.data.formData)
    const list = res.rows
    this.data.formData.total = res.total
    list.forEach(item => item.phoneStr = item.phone ? item.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '');
    this.setData({ dataList: formData.pageNum == 1 ? list : dataList.concat(list) })
  },
  /**
   * 拨打电话
   */
  callPhoneEvent(event) {
    console.log('callPhoneEvent', event)
    const phone = event.currentTarget.dataset.phone
    if (!phone) return ajax.showToast('未获得手机号')
    ajax.makePhoneCall(phone)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.formData.gardenId = options.gardenId
    if (options.gardenId) this.inputConfirm()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { formData, dataList } = this.data
    if (formData.total == dataList.length) return
    this.data.formData.pageNum += 1;
    this.getRequestList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})